package api;

import utils.HttpUtils;
import utils.SmsType;

import java.util.*;

import com.alibaba.fastjson.JSONObject;

/**
 * 短信API接口
 */
public class SmsApi extends AccessToken {
    private final static String createSignature = "/aip/sms/v1/createSignature";
    private final static String querySignature = "/aip/sms/v1/querySignature";
    private final static String querySignatureList = "/aip/sms/v1/querySignatureList";
    private final static String createTemplate = "/aip/sms/v1/createTemplate";
    private final static String deleteTemplate = "/aip/sms/v1/deleteTemplate";
    private final static String queryTemplate = "/aip/sms/v1/queryTemplate";
    private final static String queryTemplateList = "/aip/sms/v1/queryTemplateList";
    private final static String batchSendSmsTp = "/aip/sms/v1/batchSendSmsTp";

    /**
     * 1.创建短信签名
     */
    public static void createSignature(String signatureName, String remark, int smsType) {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("signatureName", signatureName);
        paramsMap.put("smsType", smsType);//1: 验证码 , 2: 行业通知 , 3:营销推广
        paramsMap.put("remark", remark);
        String res = HttpUtils.doPostJSON(host, createSignature, getCachedAccessToken(), paramsMap);
        System.out.println("创建短信签名-- >> createSignature:" + res);
    }

    /**
     * 1.查询单个短信签名
     */
    public static void querySignature(String signatureName, int smsType) {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("smsType", smsType);//1: 验证码 , 2: 行业通知 , 3:营销推广
        paramsMap.put("signatureName", signatureName);
        String res = HttpUtils.doPostJSON(host, querySignature, getCachedAccessToken(), paramsMap);
        System.out.println("查询单个短信签名-- >> querySignature:" + res);
    }

    /**
     * 1.查询短信签名列表
     */
    public static void querySignatureList() {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("status", 0);
        paramsMap.put("pageNum", 1);
        paramsMap.put("pageSize", 10);
        String res = HttpUtils.doPostJSON(host, querySignatureList, getCachedAccessToken(), paramsMap);
        System.out.println("查询短信签名列表-- >> querySignatureList:" + res);
    }

    /**
     * 1.创建短信模板
     */
    public static void createTemplate(String templateName, String remark, String templateContent, Map<String, String> templateParams, int smsType) {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("templateId", 0); //更新则id大于0
        paramsMap.put("smsType", smsType); //1: 验证码 , 2: 行业通知 , 3:营销推广
        paramsMap.put("templateName", templateName);
        paramsMap.put("templateContent", templateContent);
        paramsMap.put("templateParamJson", templateParams);
        paramsMap.put("remark", remark);
        String res = HttpUtils.doPostJSON(host, createTemplate, getCachedAccessToken(), paramsMap);
        System.out.println("创建短信模板-- >> createTemplate:" + res);
    }

    /**
     * 1.删除短信模板
     */
    public static void deleteTemplate(long templateId) {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("templateId", templateId);
        String res = HttpUtils.doPostJSON(host, deleteTemplate, getCachedAccessToken(), paramsMap);
        System.out.println("删除短信模板-- >> deleteTemplate:" + res);
    }

    /**
     * 1.查询单个短信模板
     */
    public static void queryTemplate(long templateId) {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("templateId", templateId);
        String res = HttpUtils.doPostJSON(host, queryTemplate, getCachedAccessToken(), paramsMap);
        System.out.println("查询单个短信模板-- >> queryTemplate:" + res);
    }

    /**
     * 1.查询短信模板列表
     */
    public static void queryTemplateList() {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("status", 0);
        paramsMap.put("pageNum", 1);
        paramsMap.put("pageSize", 10);
        String res = HttpUtils.doPostJSON(host, queryTemplateList, getCachedAccessToken(), paramsMap);
        System.out.println("查询短信模板列表-- >> queryTemplateList:" + res);
    }

    /**
     * 2.发送模板短信
     * 属性详情请参考文档
     */
    public static void batchSendSmsTp(String _BizNo, String signatureName, int smsType, long templateId, List<Map<String, Object>> recordList, String customId) {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("smsType", smsType);
        paramsMap.put("delaySendTime", "");
        paramsMap.put("signatureName", signatureName);
        paramsMap.put("templateId", templateId);
        paramsMap.put("recordList", recordList);
        paramsMap.put("customId", customId);
        paramsMap.put("extend", "");
        paramsMap.put("_BizNo", _BizNo);
        String res = HttpUtils.doPostJSON(host, batchSendSmsTp, getCachedAccessToken(), paramsMap);
        System.out.println("发送模板短信-- >> batchSendSmsTp:" + res);
    }


    /**
     * 短信测试
     * 1: 验证码 , 2: 行业通知 , 3:营销推广
     *
     * @param args
     */
    public static void main(String[] args) {

        smsTest1();//验证码测试
        smsTest2();//行业短信
        smsTest3();//营销短信

    }


    /**
     * 验证码
     */
    public static void smsTest1() {

        //创建短信签名
        createSignature("【源始科技】", "源始科技", SmsType.VerifyCode.getType());

        //创建短信模板
        Map<String, String> templateParams = new HashMap<String, String>() {{
            put("code", "valid_code");
        }};
        createTemplate("源始科技模板", "源始科技模板", "登录验证码：{code}，请查收", templateParams, SmsType.VerifyCode.getType());

        //查询单个签名
        querySignature("【源始科技】", SmsType.VerifyCode.getType());

        //查询短信签名列表
        querySignatureList();

        //根据模板ID查询模板
        queryTemplate(69665);

        //查询短信模板列表
        queryTemplateList();


        //发送短信
        Map<String, String> sendTemplateParams = new HashMap<String, String>() {{
            put("code", "1234");
        }};
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        recordList.add(new HashMap<String, Object>() {{
            put("mobile", "1501546xxxx");
            put("templateParams", sendTemplateParams);
        }});
        batchSendSmsTp(UUID.randomUUID().toString(), "【源始科技】", SmsType.VerifyCode.getType(), 69665, recordList, "myid001");
    }


    /**
     * 行业通知
     */
    public static void smsTest2() {
        //创建短信签名
        createSignature("【源始科技】", "源始科技", SmsType.IndustryNotify.getType());

        //创建短信模板
        Map<String, String> templateParams = new HashMap<String, String>() {{
            //put("code", "valid_code");
        }};
        createTemplate("源始科技模板", "源始科技模板", "请到深圳南山区粤海街道16号店『扫码自取』中通包裹，查看包裹服务点 https://m.tb.cn/1.1WPFAsN", templateParams, SmsType.IndustryNotify.getType());

        //查询单个签名
        querySignature("【源始科技】", SmsType.VerifyCode.getType());

        //查询短信签名列表
        querySignatureList();

        //根据模板ID查询模板
        queryTemplate(69686);

        //查询短信模板列表
        queryTemplateList();


        //发送短信
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        recordList.add(new HashMap<String, Object>() {{
            put("mobile", "1501563xxxx");
        }});
        recordList.add(new HashMap<String, Object>() {{
            put("mobile", "1501636xxxx");
        }});
        batchSendSmsTp(UUID.randomUUID().toString(), "【源始科技】", SmsType.IndustryNotify.getType(), 69682, recordList, "myid001");
    }


    /**
     * 营销推广
     * 注：需要加 退订
     */
    public static void smsTest3() {
        //创建短信签名
        createSignature("【源始科技】", "源始科技", SmsType.MarketPromotion.getType());

        //创建短信模板
        Map<String, String> templateParams = new HashMap<String, String>() {{
            //put("code", "valid_code"); //营销类型不需要传此参数
        }};
        createTemplate("源始科技模板", "源始科技模板", "尊敬的客户，为响应疫情纾困政策，您在我行可办一笔650000元的备用金，用于生意周转，申请线上办理回1；专员办理回2；退订回N", templateParams, SmsType.MarketPromotion.getType());
        //查询单个签名
        querySignature("【源始科技】", SmsType.VerifyCode.getType());

        //查询短信签名列表
        querySignatureList();

        //根据模板ID查询模板
        queryTemplate(69686);

        //查询短信模板列表
        queryTemplateList();

        //发送营销短信
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        recordList.add(new HashMap<String, Object>() {{
            put("mobile", "1501563xxxx");
        }});
        recordList.add(new HashMap<String, Object>() {{
            put("mobile", "1501636xxxx");
        }});
        batchSendSmsTp(UUID.randomUUID().toString(), "【源始科技】", SmsType.MarketPromotion.getType(), 69686, recordList, "myid001");
    }


}
